import {postUserFavoriteItineraryAddForUser,postUserFavoriteItineraryDeleteForUser} from '../../api/home'
import {imageUrl } from '../../../utils/request'
Component({
  properties: {
    itineraryDetail: {
      value: {} 
    },
    avatar: {
      type: Boolean,
      value: false 
    },
    user: {
      type: Boolean,
      value: true 
    },
    title: {
      type: String,
      value: '標題' // 默認標題
    },
    size: {
      type: String,
      value: '36rpx' // 默認標題
    },
    backShow: {
      type: Boolean,
      value: false 
    },
    backShow2: {
      type: Boolean,
      value: false 
    },
    share: {
      type: Boolean,
      value: false 
    },
    map: {
      type: Boolean,
      value: false 
    },
    collect: {
      type: Boolean,
      value: false 
    },
  },
  pageLifetimes: {
    show: function() {
      // 頁面被展示
      const systemInfo = wx.getWindowInfo();
      // const getDeviceInfo = wx.getDeviceInfo()
      const statusBarHeight = systemInfo.statusBarHeight;
      const capsuleInfo = wx.getMenuButtonBoundingClientRect();
      console.log('systemInfo',systemInfo);
      console.log('capsuleInfo',capsuleInfo);
      // if(getDeviceInfo.system.toLowerCase().includes('ios')){
      //   this.setData({
      //     ios:true
      //   })
      // }
      this.setData({
        statusBarHeight: capsuleInfo.top,
        timebar:statusBarHeight,
        navBarHeight: capsuleInfo.height,
        navHeight: capsuleInfo.bottom,
        capsuleInfo: capsuleInfo,
        capsulePaddingRight:capsuleInfo.width// 計算膠囊寬度到右側距離
      });
      // console.log(capsuleInfo);
    },
  },
  data: {
    imageUrl:'',
    statusBarHeight: 20,  // 狀態欄高度
    navBarHeight: 44,     // 導航欄高度
    capsuleInfo: {},      // 膠囊信息
    navHeight: 64,        // 整個導航欄高度
    capsulePaddingRight: 0, // 計算膠囊寬度到右側距離
    shareShow:false,
    showModal: false
  },
  lifetimes: {
    created() {
      this.setData({imageUrl})
    }
  },
  methods: {
    // 返回按鈕事件
    onBackTap() {
      const pages = getCurrentPages()
      if (pages.length > 1) {
        // 有上一頁 → 返回上一頁
        wx.navigateBack()
      } else {
        // 沒有上一頁 → 回到首頁
        wx.redirectTo({
          url: '/pages/Itinerary/Itinerary',
        })
      }
    },    
    onBackTap2() {
      wx.redirectTo({
        url: '/pages/home/home',
      })
    },
    async collectClick(){
      const res = await postUserFavoriteItineraryAddForUser({
        itinerary_id:this.properties.itineraryDetail.info.itinerary_id
      })
      wx.showToast({
        title: '收藏成功',
      })
      this.triggerEvent('refreshParent')
    },
    async collectNo(){
      const res = await postUserFavoriteItineraryDeleteForUser({
        itinerary_id:this.properties.itineraryDetail.info.itinerary_id
      })
      wx.showToast({
        title: '取消收藏',
        icon:'none'
      })
      this.triggerEvent('refreshParent')
      
    },
    shareClick(){
      this.setData({ shareShow: true });
      
    },
    closePopup() {
      this.setData({ shareShow: false });
    },
    colse(){
      this.setData({ showModal: false });
    },
    stopTap() {
      // 阻止冒泡，不讓點擊內容區關閉
    },
     // 復製方法
  copyClick(e) {
    const text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: text,
      success: () => {
        wx.showToast({
          title: '鏈接已復製',
          icon: 'none',
          duration: 2000
        });
      },
      fail: () => {
        wx.showToast({
          title: '複製失敗',
          icon: 'none'
        });
      }
    });
  },
    shareImgClick(){
      const itineraryDetail = JSON.stringify(this.properties.itineraryDetail)
      wx.navigateTo({
        url: `/pages/Itinerary/shareBanner/shareBanner?itineraryDetail=${itineraryDetail}`,
      })
    },
    // 分享给好友
    onShareAppMessage() {
      return {
        title: this.data.itineraryDetail.info.name,   // 分享标题
        path: `/pages/Itinerary/myItineraryDetails/myItineraryDetails?itinerary_id=${this.data.itineraryDetail.info.itinerary_id}`,    // 分享路径（进入页面的地址）
        imageUrl: this.data.imageURL+this.data.itineraryDetail.info.img_url_cover // 可选：自定义分享封面图
      }
    },
    onInvite() {
      wx.showToast({
        title: '已邀請',
        icon: 'success'
      });
      this.setData({ showModal: false });
    },
  }
});
